<?Lassoscript
// Last modified 4/17/09 by Eric Landmann

// FUNCTIONALITY
// Include file used in response page to add a forum

// CHANGE NOTES
// 4/17/09
// Version 1.0

// Debugging
// Var:'svDebug' = 'Y';

// Converting action_params
Var:'vError' = '';
Var:'vOption' = '';
// The action_param "Action" is passed by the frm_forum.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_forum.inc)
// Action = Add, means the request was an Add (from frm_forum.inc)
Var:'vAction' = (Action_Param:'Action');

If: $svDebug == 'Y';
	'<br>\r';
	'<strong>forum_addresponse.inc</strong><br>\r';
/If;

// Checking Action to make sure it's only posting from the Add page
// ============= ADD ========
If: $vAction == 'Add';

	// Convert action_params
	Include:($svLibsPath)'section_params.inc';

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Create name to display
		Var:'vDisplayName' = $vSection_Title;

		// Query Forum database
		// Searching for Section Title
		// If it does not exist, then add it. This avoids multiple entries of the same title
		Var:'SQLSearchSections'='SELECT Section_Title from ' $svSectionsTable ' WHERE Section_Title = "' $vSection_Title '"';
		Inline: $IV_SearchSections, -SQL=$SQLSearchSections;
	
			Debug;
				'27: SQLSearchSections = ' $SQLSearchSections '<br>\r';
				'27: Found_Count =  ' (Found_Count) '<br>\r';
			/Debug;

			// If no forum found, add the record
			If: (Found_Count) == 0;
	
				// Construct the query to add the Forum
				Var:'vSQLAddSection' = '
				INSERT INTO ' $svSectionsTable '  (
					Section_ID,
					Section_TItle,
					Request_Info,
					Allow,
					Section_Description
					) 
				VALUES ("",
					"'(Encode_SQL:(Var:'vSection_Title'))'",
					"'(Encode_SQL:(Var:'vRequest_Info'))'",
					"'(Encode_SQL:(Var:'vAllow'))'",
					"'(Encode_SQL:(Var:'vSection_Description'))'"
					);';
			
						Debug;
							'128: vSQLAddSection = ' $vSQLAddSection '<br>\n';
						/Debug;
			
				Inline: $IV_AddSection, -SQL=$vSQLAddSection;
					
					If:(Error_CurrentError)==(Error_NoError);
				
						// Get new record ID value from LAST_INSERT_ID() for link
						Inline: -SQL='SELECT LAST_INSERT_ID() AS NewSectionID';
							Var:'vNewSectionID' = (Field:'NewSectionID');
						/Inline;


						// Add the Stats record
						Var:'vSQAddSectionToStats' = 
							'INSERT INTO ' $svStatsTable ' (
								Section_ID,
								The_Count,
								Last_Post_Date
								)
							VALUES ( 
							"'(Var:'vNewSectionID')'",
							"'(Var:'New_Count')'",
							"'(Server_Date:-Extended)'"
							);';
	
						Inline: $IV_UpdateStats, -SQL=$vSQAddSectionToStats; /Inline;


						// If no error, Add was successful, dump out Error 2001 "Add Successful"
						Var:'vError' = '2001';
						Var:'vOption' = 'forum';

					// There was an error
					Else;
						Var:'vError' = (Error_CurrentError);
					/If;
				
				/Inline;
			
			// Forum found in Sections table, output 2006 "Duplicate Forum" error
			Else;
				Var:'vError' = '2006';
			/If;	

		/Inline;

	/If;

	Debug;
		'137: vError = ' $vError '<br>\n';
		'137: vOption = ' $vOption '<br>\n';
	/Debug;
// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else: $vAction == 'Update';

	// Convert action_params
	Include:($svLibsPath)'section_params.inc';

	// Copying  record ID to vNewSectionID to build link
	// This is so we can just use vNewSectionID at bottom of page for both add and edit
	Var:'vNewSectionID'=(Var:'vSID');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Construct the query to Update the Section
		// Not changing the Date_Created fields
		Var:'vSQLUpdateSections' = '
		UPDATE ' $svSectionsTable ' SET
			Section_ID = "'(Encode_SQL:(Var:'vSID'))'",
			Section_Title = "'(Encode_SQL:(Var:'vSection_Title'))'",
			Request_Info = "'(Encode_SQL:(Var:'vRequest_Info'))'",
			Allow = "'(Encode_SQL:(Var:'vAllow'))'",
			Section_Description = "'(Encode_SQL:(Var:'vSection_Description'))'"
			WHERE Section_ID = "' $vSID '"';

		Debug;
			'224: vSQLUpdateSections = ' $vSQLUpdateSections '<br>\n';
		/Debug;
	
		Inline: $IV_UpdateSections, -SQL=$vSQLUpdateSections;
			
			If:(Error_CurrentError)==(Error_NoError);
		
				// If no error, Update was successful, dump out Error 5022 "Update Successful"
				Var:'vError' = '5022';
				Var:'vOption' = 'Forum';
				Debug;
					'233: vError = ' $vError '<br>\n';
					'233: vOption = ' $vOption '<br>\n';
				/Debug;
		
			// There was an error
			Else;
				Var:'vError' = (Error_CurrentError);
			/If;
		
		/Inline;

		// vError == ''
		/If;
		
// Referrer not good, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	Var:'vError' = '9002';
/If;


// Deal with the results of the processing

// ============= ADD ========
// If any other error other than 2001 "Add Successful" do a redirect
If: $vAction == 'Add';
	// Standard Error Table
	If: (Var:'vError') == '2001';
		LI_ShowError3: -ErrNum=$vError, -Option=$vOption;


// Edit New Listing Link
?>
<div align="left">
	View the record for <a href="setup_editrecord.lasso?DataType=Forum&SID=[Var:'vNewSectionID']&New=Y"><strong>[$vSection_Title]</strong></a>
</div>
<?Lassoscript
	Else;
		Var:'URL' = ('setup_add.lasso?Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'273: URL = ' $URL '<br>';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
/If;

// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 5022 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If: $vAction == 'Update';
	If: (Var:'vError') == '5022';
		Var:'URL' = ('setup_editrecord.lasso?DataType=Forum&SID='$vNewSectionID'&New=Y&Error='$vError'&Option='$vOption);
	Else;
		Var:'URL' = ('setup_editrecord.lasso?New=Y&Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
	/If;
	If: $svDebug == 'Y';
		Debug;
			'294: URL = ' $URL '<br>';
			'<a href="'$URL'">' $URL '</a><br>\n';
		/Debug;
	Else;
		Redirect_URL: $URL;
	/If;
/If;
?>
